package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.pojos.RTagSource;
import com.jilefojing.beans.query.RTagSourceQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IRTagSourceDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IRTagSourceMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月25日 22时06分59秒
 */
@Repository
public class RTagSourceDao extends BaseDao<RTagSource> implements IRTagSourceDao {
	@Autowired
	private IRTagSourceMapper rTagSourceMapper;

	@Override
	public Pagination<RTagSource> getPagination(RTagSourceQuery rTagSourceQuery, Pagination<RTagSource> pagination) {
		Example example = new Example(RTagSource.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(rTagSourceQuery.getIds())) {
			criteria.andIn(RTagSource.FIELD_ID, rTagSourceQuery.getIds());
		}

		if (Detect.notEmpty(rTagSourceQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _ItemType : rTagSourceQuery.getItemTypes()) {
				itemTypeCodes.add(_ItemType.getCode());
			}
			criteria.andIn(RTagSource.FIELD_ITEMTYPE, itemTypeCodes);
		}

		if (Detect.notEmpty(rTagSourceQuery.getStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (StatusEnumForPublish _status : rTagSourceQuery.getStatuses()) {
				statusCodes.add(_status.getCode());
			}
			criteria.andIn(RTagSource.FIELD_STATUS, statusCodes);
		}

		if (Detect.notEmpty(rTagSourceQuery.getItemIds())) {
			criteria.andIn(RTagSource.FIELD_ITEMID, rTagSourceQuery.getItemIds());
		}
		if (Detect.notEmpty(rTagSourceQuery.getTagIds())) {
			criteria.andIn(RTagSource.FIELD_TAGID, rTagSourceQuery.getTagIds());
		}
		if (Detect.notEmpty(rTagSourceQuery.getCreateUserIds())) {
			criteria.andIn(RTagSource.FIELD_CREATEUSER, rTagSourceQuery.getCreateUserIds());
		}

		if (rTagSourceQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(RTagSource.FIELD_CREATETIME, rTagSourceQuery.getEqualCreateTime());
		}

		if (rTagSourceQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(RTagSource.FIELD_CREATETIME, rTagSourceQuery.getEqualAndBeforCreateTime());
		}

		if (rTagSourceQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(RTagSource.FIELD_CREATETIME, rTagSourceQuery.getEqualAndAfterCreateTime());
		}

		if (rTagSourceQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(RTagSource.FIELD_CREATETIME, rTagSourceQuery.getBeforCreateTime());
		}

		if (rTagSourceQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(RTagSource.FIELD_CREATETIME, rTagSourceQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}

	@Override
	public List<Map<String, Object>> countSourcesByItemTypesForTags(List<Long> tagIds, List<ItemTypeEnumForSource> itemTypes) {
		return rTagSourceMapper.countSourcesByItemTypesForTags(tagIds, itemTypes);
	}

	@Override
	public List<RTagSource> getListByItem(ItemTypeEnumForSource itemType, long itemId) {
		return this.getListByItems(itemType, Arrays.asList(new Long[] { itemId }));
	}

	@Override
	public List<RTagSource> getListByItems(ItemTypeEnumForSource itemType, List<Long> itemIds) {
		Example example = new Example(RTagSource.class);
		example.createCriteria().andEqualTo(RTagSource.FIELD_ITEMTYPE, itemType.getCode()).andIn(RTagSource.FIELD_ITEMID, itemIds);
		return super.getListByExample(example);
	}

	@Override
	public void deleteByItem(ItemTypeEnumForSource itemType, long itemId) {
		this.deleteByItems(itemType, Arrays.asList(new Long[] { itemId }));
	}

	@Override
	public void deleteByItems(ItemTypeEnumForSource itemType, List<Long> itemIds) {
		Example example = new Example(RTagSource.class);
		example.createCriteria().andEqualTo(RTagSource.FIELD_ITEMTYPE, itemType.getCode()).andIn(RTagSource.FIELD_ITEMID, itemIds);
		super.deleteByExample(example);
	}

	@Override
	public long countByTagId(long tagId) {
		return this.countByTagIds(Arrays.asList(new Long[] { tagId }));
	}

	@Override
	public long countByTagIds(List<Long> tagIds) {
		Example example = new Example(RTagSource.class);
		example.createCriteria().andIn(RTagSource.FIELD_TAGID, tagIds);
		return super.countByExample(example);
	}

	@Override
	public void deleteByTagId(long tagId) {
		this.deleteByTagIds(Arrays.asList(new Long[] { tagId }));
	}

	@Override
	public void deleteByTagIds(List<Long> tagIds) {
		Example example = new Example(RTagSource.class);
		example.createCriteria().andIn(RTagSource.FIELD_TAGID, tagIds);
		super.deleteByExample(example);
	}
}
